package com.example.jchuah.tiles;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import java.util.Random;


public class MyActivity extends Activity implements View.OnTouchListener, View.OnDragListener, FlingDetector.FlingListener {

    int numRows = 4;
    int numCols = 4;

    TileGridLayout grid;
    Random randGen = new Random();
    FlingDetector flingDetector = new FlingDetector(this);
    GestureDetector gestureDetector;


    public void swaptiles(RowColPair source, RowColPair dest) {

        Log.d("filing event", "swap tiles");

        if (grid.isValidRowColPair(dest)) {
            View view = grid.getTile(source);
            View justremoved = grid.getTile(dest);

             if (view.getTag().equals(justremoved.getTag())) {
                grid.removeTile(dest);
                grid.removeTile(source);
                int id = 0;
                id = R.layout.blanktile;


                View tile = getLayoutInflater()
                        .inflate(id, null);
                tile.setTag(id);

                 View tile2 = getLayoutInflater()
                         .inflate(id, null);
                 tile2.setTag(id);



                 grid.addTile(dest,tile);
                 grid.addTile(source,tile2);


            }
            else{
                 grid.removeTile(dest);
                 grid.removeTile(source);
                 grid.addTile(dest, view);
                 grid.addTile(source, justremoved);
             }
        }
    }



    public void onFlingLeft(View view) {
        Log.d("fling event", "swipe left");

        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair leftof = coordinate.getLeft();
        swaptiles(coordinate,leftof);


    }


    public void onFlingRight(View view) {
        Log.d("fling event", "swipe right");

        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair rightof = coordinate.getRight();
        swaptiles(coordinate,rightof);




    }

    public void onFlingUp(View view) {
        Log.d("fling event", "swipe up");

        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair above = coordinate.getAbove();
        swaptiles(coordinate,above);


    }

    public void onFlingDown(View view) {
        Log.d("fling event", "swipe down");

        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair downof = coordinate.getBelow();
        swaptiles(coordinate,downof);

    }
    public void generateGrid() {
        for (int row = 0; row < numRows; row++){
            for (int col = 0; col < numCols; col++) {

                int type = col % 2;
                int id = 0;


                if(type == 0){

                    id = R.layout.tile;

                }


                if(type == 1){

                    id = R.layout.tile2;

                }

                grid.addTile(row, col, generateTile(id));



            }
        }
    }

    public View generateTile(int id){
        View tile = getLayoutInflater()
                    .inflate(id, null);
        tile.setTag(id);

        tile.setOnTouchListener(new View.OnTouchListener(){
            @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
                flingDetector.touched = view;
                return
    gestureDetector.onTouchEvent(motionEvent);
            }
        });
        return tile;
    }

    public void checkvictory(){
        int numminions = 0;
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {
                RowColPair p = new RowColPair();
                p.row = row;
                p.col = col;
                View tile = grid.getTile(p);
                if (tile.getTag().equals(R.layout.blanktile)) {
                    numminions++;
                }
            }
        }
        if (numminions == 16){
            //Log.d
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        grid = (TileGridLayout)findViewById(R.id.gridView);
        generateGrid();
        gestureDetector = new GestureDetector(this, flingDetector);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        return false;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }

}
